package com.vois.jack.btmgr.blebase;

import android.bluetooth.BluetoothDevice;
import android.os.Parcel;
import android.os.Parcelable;

public class BleScanResult implements Parcelable {
	private BleScanRecord bleScanRecord;
	private BluetoothDevice bluetoothDevice;
	private int rssi;

	public BleScanResult(byte[] scanRecordData, BluetoothDevice bluetoothDevice, int rssi) {
		this.bleScanRecord = BleScanRecord.parseFromBytes(scanRecordData);
		this.bluetoothDevice = bluetoothDevice;
		this.rssi = rssi;
	}

	public BleScanRecord getBleScanRecord() {
		return bleScanRecord;
	}

	public void setBleScanRecord(byte[] scanRecordData) {
		this.bleScanRecord = BleScanRecord.parseFromBytes(scanRecordData);
	}

	public BluetoothDevice getBluetoothDevice() {
		return bluetoothDevice;
	}

	public void setBluetoothDevice(BluetoothDevice bluetoothDevice) {
		this.bluetoothDevice = bluetoothDevice;
	}

	public int getRssi() {
		return rssi;
	}

	public void setRssi(int rssi) {
		this.rssi = rssi;
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		bluetoothDevice.writeToParcel(dest, flags);
		if (bleScanRecord != null) {
			dest.writeInt(1);
			dest.writeByteArray(bleScanRecord.getBytes());
		} else {
			dest.writeInt(0);
		}
		dest.writeInt(rssi);
	}

	private void readFromParcel(final Parcel in) {
		bluetoothDevice = BluetoothDevice.CREATOR.createFromParcel(in);
		if (in.readInt() == 1) {
			bleScanRecord = BleScanRecord.parseFromBytes(in.createByteArray());
		}
		rssi = in.readInt();
	}

	private BleScanResult(final Parcel in) {
		readFromParcel(in);
	}

	public static final Parcelable.Creator<BleScanResult> CREATOR = new Creator<BleScanResult>() {
		@Override
		public BleScanResult createFromParcel(final Parcel source) {
			return new BleScanResult(source);
		}

		@Override
		public BleScanResult[] newArray(final int size) {
			return new BleScanResult[size];
		}
	};
}